﻿using System;

namespace CodeContrib.Tasks
{
	public abstract class Task
	{
		public abstract void Execute();

		protected virtual void OnError(Exception exception)
		{
		}

		public bool? Run()
		{
			try
			{
				Execute();

				TaskExecutor.StartExecuting();

				return true;
			}
			catch (Exception exception)
			{
				OnError(exception);

				return false;
			}
			finally
			{
				TaskExecutor.Discard();
			}
		}
	}
}